<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="账号" prop="account">
        <el-input
          v-model="queryParams.account"
          placeholder="请输入账号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="密码" prop="passWord">
        <el-input
          v-model="queryParams.passWord"
          placeholder="请输入密码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="搜索id" prop="userSelectId">
        <el-input
          v-model="queryParams.userSelectId"
          placeholder="请输入搜索id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="积分" prop="integration">
        <el-input
          v-model="queryParams.integration"
          placeholder="请输入积分"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ryzs:ryzs:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ryzs:ryzs:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ryzs:ryzs:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ryzs:ryzs:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
     <el-divider></el-divider>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-circle-plus"
          size="mini"
          @click="invitationBach"
        >批量注册二十个</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-circle-plus"
          size="mini"
          @click="batchSignInBatch"
        >一键登录签到养号</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="exportQuantity"
        >导出卡密</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="ryzsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="账号" align="center" prop="account" />
      <el-table-column label="密码" align="center" prop="passWord" />
      <el-table-column label="搜索id" align="center" prop="userSelectId" />
      <el-table-column label="积分" align="center" prop="integration" />
      <el-table-column label="用户类型" align="center" prop="userType" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="累计签到天数" align="center" prop="signInDays" />
      <el-table-column label="最新签到时间" align="center" prop="signInTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ryzs:ryzs:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ryzs:ryzs:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改人鱼助手对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="账号" prop="account">
          <el-input v-model="form.account" placeholder="请输入账号" />
        </el-form-item>
        <el-form-item label="密码" prop="passWord">
          <el-input v-model="form.passWord" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="搜索id" prop="userSelectId">
          <el-input v-model="form.userSelectId" placeholder="请输入搜索id" />
        </el-form-item>
         <el-form-item label="用户类型" prop="userType">
          <el-input v-model="form.userType" placeholder="请输入用户类型" />
        </el-form-item>
        <el-form-item label="积分" prop="integration">
          <el-input v-model="form.integration" placeholder="请输入积分" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

        <!-- 添加或修改人鱼助手对话框 -->
    <el-dialog :title="exportParams.title" :visible.sync="exportParams.open" width="500px" append-to-body>
      <el-form ref="thisform" :model="exportParams" :rules="rules" label-width="80px">
        <el-form-item label="导出数量" prop="exportQuantity">
          <el-input v-model="exportParams.exportQuantity" placeholder="请输入导出数量" />
        </el-form-item>
        <el-form-item label="导出积分" prop="exportIntegration">
          <el-input v-model="exportParams.exportIntegration" placeholder="请输入导出积分" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleExportQuantity">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRyzs, getRyzs, delRyzs, addRyzs, updateRyzs, invitation, batchSignIn } from "@/api/ryzs/ryzs";

export default {
  name: "Ryzs",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 人鱼助手表格数据
      ryzsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        account: null,
        passWord: null,
        userSelectId: null,
        integration: null,
        userType: null,
      },
      // 导出卡密参数
      exportParams: {
        // 是否显示弹出层（导出卡密）
        open: false,
        // 弹出层标题（导出卡密）
        title: "",
        exportQuantity: null,
        exportIntegration: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询人鱼助手列表 */
    getList() {
      this.loading = true;
      listRyzs(this.queryParams).then(response => {
        this.ryzsList = response.rows;
        this.total = response.total;
      }).finally(() => {
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();

      this.exportParams = {};
      this.resetForm("thisform");
      this.exportParams.open = false;
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加人鱼助手";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRyzs(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改人鱼助手";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRyzs(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRyzs(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      if (row.id || (!row.id && !this.single)) {
        this.$prompt('请输入删除人鱼助手编号为"' + ids + '"的数据项原因', "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          closeOnClickModal: false
        }).then(({ value }) => {
          this.form.remark = value;
          this.form.id = ids;
          updateRyzs(this.form)
          return delRyzs(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      } else {
        this.$modal.confirm('是否确认删除人鱼助手编号为"' + ids + '"的数据项？').then(function() {
            return delRyzs(ids);
          }).then(() => {
            this.getList();
            this.$modal.msgSuccess("删除成功");
          }).catch(() => {});
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ryzs/ryzs/export', {
        ...this.queryParams
      }, `ryzs_${new Date().getTime()}.xlsx`)
    },
    /** 导出卡密按钮操作 */
    exportQuantity() {
      this.exportParams = {};
      this.resetForm("thisform");
      this.exportParams.open = true;
      this.exportParams.title = "导出卡密";
    },
    handleExportQuantity() {
      this.exportParams.open = false;
      this.download('ryzs/ryzs/exportQuantity', {
        ...this.exportParams
      }, `ryzs_${new Date().getTime()}.xlsx`)
      
    },
    /** 批量注册按钮操作 */
    invitationBach() {
      invitation().then(response => {
        this.$modal.msgSuccess("批量注册小号成功");
         this.loading = true;
         setTimeout(() => {
           this.getList();
         }, 1500);
      });
    },
    /** 批量登录签到按钮操作 */
    batchSignInBatch() {
      batchSignIn().then(response => {
        if(response.data === 0) {
          this.$modal.msgSuccess("获取到" + response.data + "个账号需要养号签到，后台运行中...");
        } else {
          this.$modal.msgSuccess("批量养号-登录签到成功，后台运行中...");
        }
        console.log(response,"1111");
        this.getList();
      });
    }
  }
};
</script>
